using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Microsoft.Modeling.StoryboardDesigner
{
    partial class CommentShape
    {
        #region Constants that must match values in the Dsl Definition
        /// <summary>
        /// Name of the Title field (set in the Dsl Definition)
        /// </summary>
        private const string TitleFieldName = "CommentText";
        #endregion

        // Stores the current collapsed size of the shapes
        private SizeD defaultSize;

        /// <summary>
        /// Override: replaces the Title TextField with an AutoSizeTextField.
        /// </summary>
        /// <remarks>Necessary because the sizing behaviour of TextField isn't
        /// quite what we want: all instances of the shape field are sized based
        /// on whichever shape was last changed. We want the size of the shape field
        /// to vary for each shape it is associated with.</remarks>
        protected override void InitializeShapeFields(IList<ShapeField> shapeFields)
        {
            base.InitializeShapeFields(shapeFields);

            // Now replace the Title TextField with an 
            // AutoSizeTextField.
            TextField titleField = FindShapeField(shapeFields, TitleFieldName) as TextField;
            Debug.Assert(titleField != null, "Error - could not find the Title text field. Has the Dsl Definition changed?");

            AutoSizeTextField newTitleField = CreateAutosizeTextField(TitleFieldName);
            shapeFields.Remove(titleField);
            shapeFields.Add(newTitleField);
            // NB must add the shape field after the area shape field, otherwise
            // the title field will be painted behind the area field, and so 
            // won't be visible.
        }

        /// <summary>
        /// Override: changes the anchoring behaviour of the Title field.
        /// </summary>
        /// <remarks>The base class sets the generated anchoring behaviour, which
        /// we then change.</remarks>
        protected override void InitializeDecorators(IList<ShapeField> shapeFields, IList<Decorator> decorators)
        {
            base.InitializeDecorators(shapeFields, decorators);

            // Now change the title field anchoring to suit us.
            TextField titleField = FindShapeField(shapeFields, TitleFieldName) as TextField;
            Debug.Assert(titleField != null, "Error - could not find the Title text field. Has the Dsl Definition changed?");

            SetTitleFieldAnchoring(shapeFields, titleField);
        }

        /// <summary>
        /// Creates and returns a new instance of AutoSizeTextField that 
        /// will display multiline and can be edited.
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        private static AutoSizeTextField CreateAutosizeTextField(string fieldName)
        {
            AutoSizeTextField field = new AutoSizeTextField(fieldName);

            field.DefaultFocusable = true;
            field.DefaultText = "";
            field.DefaultMultipleLine = true;
            field.DefaultAutoSize = false;

            StringFormat format1 = new StringFormat(StringFormatFlags.NoClip);
            format1.Trimming = StringTrimming.EllipsisWord;
            format1.Alignment = StringAlignment.Near;
            format1.LineAlignment = StringAlignment.Near;

            field.DefaultStringFormat = format1;

            return field;
        }

        /// <summary>
        /// Set the anchoring for the Title text field
        /// </summary>
        /// <remarks>Anchor the top to the top of the parent;
        /// Left to the left of the parent;
        /// Right to the left of the expand collapse decorator;
        /// Bottom to the top of the first compartment shape.</remarks>
        private static void SetTitleFieldAnchoring(IList<ShapeField> shapeFields, TextField titleField)
        {
            titleField.AnchoringBehavior.Clear();
            titleField.AnchoringBehavior.SetTopAnchor(AnchoringBehavior.Edge.Top, 0.1);
            titleField.AnchoringBehavior.SetLeftAnchor(AnchoringBehavior.Edge.Left, 0.05);
            titleField.AnchoringBehavior.SetRightAnchor(AnchoringBehavior.Edge.Right, 0.05);
        }

        public override SizeD DefaultSize
        {
            get
            {
                if(defaultSize == SizeD.Empty)
                {
                    defaultSize = base.DefaultSize;
                }

                return defaultSize;
            }
        }
    }
}